package com.sysmanagment.dao.user;

import javax.persistence.NonUniqueResultException;

import org.jboss.seam.annotations.Name;

import com.sysmanagment.dao.DAOImpl;
import com.sysmanagment.model.user.User;

@Name(value = "userDAO")
public class UserDAOImpl extends DAOImpl<User> implements UserDAO {

	@Override
	public User validateUser(String username, String passwordHash) {
		String query = "select u from User u where u.username = :username and u.passwordHash = :passwordHash  and u.enabled=1";
		User loggedUser;
		try {
			loggedUser = (User) entityManager.createQuery(query).setParameter(
					"username", username).setParameter("passwordHash",
					passwordHash).getSingleResult();
		} catch (NonUniqueResultException e) {
			return loggedUser = null;
		} catch (Exception e) {
			return loggedUser = null;
		}
		return loggedUser;
	}

	@Override
	public User getUser(String username) {
		String query = "select u from User u where u.username = :username";
		User user;
		try {
			user = (User) entityManager.createQuery(query).setParameter(
					"username", username).getSingleResult();
		} catch (NonUniqueResultException e) {
			return user = null;
		} catch (Exception e) {
			return user = null;
		}
		return user;
	}

	@Override
	protected Class<User> getEntityClass() {
		// TODO Auto-generated method stub
		return User.class;
	}

}
